<template>
  <div class="home-container">
    <el-container>
      <el-header>
        <div class="header-left">
          <img src="../assets/heima.png">
          <span>前端16期后台管理系统</span>
        </div>
        <el-button @click="logout">退出</el-button>
      </el-header>
      <!-- 页面组体 -->
      <el-container>
        <!-- 侧边栏区域 -->
        <el-aside :width="isCollapse ? '64px' : '200px'">
          <!-- 收缩面包屑按钮 -->
          <div @click="isCollapse=!isCollapse" class="toggleMenu">|||</div>
          <el-menu
          class="el-menu-vertical-demo"
          background-color="#545c64"
          text-color="#fff"
          :unique-opened='true'
          :collapse='isCollapse'
          :collapse-transition='false'
          router
          :default-active='$route.path'
          active-text-color="#409eff">
          <!-- 一级分类 -->
            <el-submenu v-for="item in menulist" :key="item.id" :index="item.path+ ''">
              <!-- 一级导航 -->
              <template slot="title">
                <!-- 字体图标 -->
                <i :class="iconsList[item.id]"></i>
                <span>{{item.authName}}</span>
              </template>
              <!-- 二级分类 -->
              <el-menu-item v-for="item2 in item.children" :key="item2.id" :index="'/' + item2.path">
                <!-- 二级导航 -->
                <template slot="title">
                  <!-- 字体图标 -->
                  <i class="el-icon-menu"></i>
                  <span>{{item2.authName}}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main>
          <!-- 路由占为 -->
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 左侧菜单列表
      menulist: [],
      // 菜单图标
      iconsList: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      // 控制菜单导航是否折叠
      isCollapse: false
      // 控制当前激活
    }
  },

  created () {
    this.getMenulist()
  },

  methods: {
    // 点退出
    logout () {
      sessionStorage.removeItem('token')
      this.$router.push('/login')
    },
    // 获取菜单数据
    async getMenulist () {
      const { data: res } = await this.$axios.get('menus')
      if (res.meta.status !== 200) return this.$message.error('获取数据失败')
      this.menulist = res.data
    }
  }
}
</script>

<style scoped lang='less'>
  .home-container {
    height: 100%;
    .el-container {
      height: 100%;
    }
    .el-header {
      background: #373d41;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .header-left {
        display: flex;
        align-items: center;
        span {
          font-size: 30px;
          color: #fff;
          margin-left: 10px;
        }
      }
    }
    .el-aside {
      background: #373d41;
      .iconfont {
        padding: 10px;
      }
      .el-menu {
        border-right: none;
      }
      .toggleMenu {
        width: 100%;
        height: 40px;
        line-height: 40px;
        text-align: center;
        background: peachpuff;
        color: #fff;
        letter-spacing: 5px;
        cursor: pointer;
        user-select: none;
      }
    }
    .el-main {
      background: #eaedf1
    }
  }
</style>
